<?php 
## include required files
/*******************************/

require_once '../model/users.php';
require_once '../model/contactus.php'; 
require_once '../model/email.php'; 
require_once '../includes/functions.php';
/*******************************/
## Create Objects 
/*******************************/
$Obj =  new Model_Contactus();	
$mailObj=new Model_Email();
$userObj=new Model_Users(); 
/*******************************/

$userArray = $userObj->getUserDetailsByUserId($_SESSION['admin_id']);

if($_GET['id']!='') 
{
	$con_id = base64_decode($_GET['id']);
	$smarty->assign('page', $_GET['page']);
	$contact_array=$Obj->getContactus($con_id);
	$smarty->assign("contact_array",$contact_array);
}
$edit_array['readflag']='read';
$Obj->editContactus($edit_array,$con_id);

if(isset($_POST['Send']) && $_POST['Send']=='Send')
{
	$replyArray=array();
	$replyArray['subject_reply']	=	$_POST['subject']	;
	$replyArray["reply_date"] 		= 	date("Y-m-d H:i:s"); 	
	$replyArray['contact_id'] 		= 	$con_id;
	$replyArray['message_reply']	=	$_POST['message'];
	$rep_id	=	$Obj->addContactUsReply($replyArray); 
	
	$contactArray	=	array();
	$contactArray['status']	= 'answered';
	$Obj->editContactus($contactArray,$con_id);
	
	/*$message	= str_replace('[SITENAME]', SITENAME, $message);
	$message  	= str_replace('[NAME]', (ucfirst($contact_array['name'])) , $message);
	$message  	= str_replace('[REPLY]', ucfirst(nl2br($_POST['message'])) , $message);
	$message	= str_replace('[SITE_LINK]', SITE_URL, $message);*/

	
	$template_msg = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
						"http://www.w3.org/TR/html4/loose.dtd">
						<html>
						<head>
						<title>Welcome</title>
						<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
						<link href="'.SITE_URL.'templates/default/css/layout.css" rel="stylesheet" type="text/css">
						</head>
						<body>
							<div style="margin:20px auto 0 auto;border:10px solid #CCC; width:600px;">
								<table cellpadding="10" cellspacing="0" width="100%">
									<tr>
										<td bgcolor="#FFFBF0"><img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'"></td>
									</tr>
								   
									<tr><td style="font-size:13px;">'.ucfirst(nl2br($_POST['message'])).'</td></tr>
									<tr><td style="font-size:13px;">'.ucfirst(nl2br($_POST['sign'])).'</td></tr>
								</table>
							</div>
						</body>
						</html>';
						
	$to	=	$contact_array['email_id'];
	$from 				=$userArray['email'];
	$fromname 			= TITLE;	
	$subject = $_POST['subject']; 
	//echo $to;echo"<br>";echo $template_msg;echo"<br>";echo $subject;echo"<br>";echo $from;echo"<br>";echo $fromname;echo"<br>";exit;
	@send_mail($to,$template_msg,$subject,$from, $fromname);
	$_SESSION['msg']="Email sent Successfully"; 
	header("location:".SITE_URL."/admin/home.php?q=detail_contact&id=".base64_encode($con_id));
}


## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'contact');
$smarty->assign('mainmenu', '3');

## Unset all the objects created which are on this page
unset($Obj);
unset($moduleObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'contact/detail_contact.tpl');
unset($smarty);
?>